var itemIndex = [];

function getElementsByClassName(className) {
    var elements = [];

    if (!document.getElementsByClassName) {
        var domNodes = document.getElementsByTagName("*");
        for (var i = 0; i < domNodes.length; i++) {
            if (domNodes[i].className) {
                var classNames = domNodes[i].className.split(" "); /*这里其实还要考虑类名间隔大于一个空格的情况*/
                for (var j = 0; j < classNames.length; j++) {
                    if (classNames[j] == className) {
                        elements.push(domNodes[i]);
                    }
                }
            }
        }
    } else {
        /*这里既然浏览器支持这个行为，为何不直接return document.getElementsByClassName(className) ？？？*/
        var domNodes = document.getElementsByClassName(className);
        for (var i = 0; i < domNodes.length; i++) {
            elements.push(domNodes[i]);
        }
    }
    return elements;
}

(function getDatas() {
    var ajax;
    if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
    } else {
        ajax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    ajax.open('post', api + daylist, true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send('base_id=' + getCookie('base_id'));
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4 && ajax.status == 200) {
            var text = ajax.responseText;
            if (text.length != 0) {
                var response1 = eval("(" + text + ")");
            }
            var str = "";
            var response2 = response1.lists;
            for (var i = 0; i < response2.length; i++) {

                if(i == 0){
                    str += "<div id=" + response2[i].id + " class='list-li' style='background: url(\"./images/day-li-active.png\");background-size: 100% 100%;'>" + response2[i].name + "</div>";
                }else{
                    str += "<div id=" + response2[i].id + " class='list-li'>" + response2[i].name + "</div>";

                }
                
                itemIndex.push(response2[i].id);
                // if(i==0){
                //     document.getElementById(itemIndex[0]).style.backgroundImage="url('./images/day-li-active.png')";
                // }
            }
        }
        document.getElementById("monthlist").innerHTML = str;
    }
})();

var backurl = getParam('backurl')
console.log(itemIndex, "ssss")

//获取机顶盒或浏览器信息
var env = evm.getStbBrowser();

function cookie(k, v) {
    if (v != undefined && typeof v == 'string') {
        evm.cookie(k, v);
        return;
    }
    if (!!evm.cookie(k)) {
        return evm.cookie(k);
    }
}


/*
 * $cmsIndex首页视图模块
 * 依赖:
 */
var cmsIndex = (function () {
    var mod = {};

    //配置项
    var config = mod.config = {
        //开启cookie开关
        cookieSwitch: true,
        cookieKey: 'cmsIndex',
        //模拟数据配置
        mockData: {
            //URI查询标示符
            key: '[mdata]',
            //文件目录
            dir: '../source/mockData'
        },
        //调试输出开关
        debug: false
    };

    var localData = mod.localData = {
        indexSource: '[mdata]/index.js'
    };
    //接口数据缓存
    var jsonCache = {};

    //各模块焦点切换的配置
    var modules = mod.modules = [];

    //当前模块的索引值,被引用的模块
    moduleId = 0,
        module = null,
        moduleGuid = 0,
        moduleCount = -1;

    mod.addModule = function () {
        return moduleCount += 1;
    };

    //moduleGuid存取器
    mod.moduleGuid = function (id) {
        if (typeof id == 'number') moduleGuid = id;
        return moduleGuid;
    };

    if (config.cookieSwitch && config.cookieKey) {
        //read cookie
        var pageCookie = cookie(config.cookieKey);
        if (pageCookie) {
            pageCookie = pageCookie.split(',');

            if (pageCookie.length > 0) {
                var moduleCookie = mod.moduleCookie = pageCookie[0].split('.');

                if (pageCookie[1]) {
                    var viewportCookie = mod.viewportCookie = moduleCookie;
                    moduleCookie = mod.moduleCookie = pageCookie[1].split('.');

                }
                if (pageCookie[2]) {
                    var parentCookie = mod.parentCookie = pageCookie[2].split('.');
                }
            }
        }

        //清除cookie
        evm.cutCookie(config.cookieKey);
    }
    //添加视图按键管理器
    mod.addListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'add',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(mod)
    //移除视图按键管理器
    mod.removeListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'remove',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(module)
    //视图按键管理

    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;

        if (module && module.grabEvent) {
            isBlock = module.grabEvent(key) || 0;
        }

        return isBlock;
    };
    //模块存取器
    mod.module = function (mID) {
        if (modules[mID]) {
            module = modules[mID].module || null;
        }

        return module;
    };

    //切换当前模块
    mod.handle = function (directive) {
        var me = mod;
        if (modules[moduleId].map &&
            (typeof modules[moduleId].map[directive] == 'number')) {

            moduleId = modules[moduleId].map[directive];

            return me.module(moduleId);
        }
    };
    mod.paint = function () {
        var me = mod;

        me.addListener();
        return me;
    };

    mod.run = function () {
        var me = mod;
        //默认moduleId = 0 导航栏
        //cookie得到焦点
        // getDatas();
        var nav = mod.module(moduleId);
        cmsIndex.handle('right1');
        right1.toggleFocus();
        //控制器管理按键
        evm.on(document, (!env.stb ? 'keydown' : (env.stb == 'iPanel' ?
            'irkeypress' : 'keydown')), evm.controller, evm);

        me.paint();

        return me;
    };

    return mod;

})();


/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right1',
    modNum: cmsIndex.moduleGuid(),
    map: {
        bottom1: 1
    }
};

var right1 = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //右侧选中地址位置
        listName = 'list-li',
        navName = 'navContain',
        length = 15,
        containerId = 'right',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'huodong';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function () {
        // alert(index);
        var me = mod;
        if(itemIndex&&itemIndex[0]){
            document.getElementById(itemIndex[0]).style.backgroundImage="url('./images/day-li-active.png')";
        }
        // var elem = evm.$('monthlist').getElementsByClassName(listName);
        // for (i = 0; i < length; i++) {
        //     elem[i].className = listName;
        // } 
        // elem[index].className = elem[index].className + ' active';
        if(itemIndex){
            for(var i =0;i<15;i++){
                document.getElementById(itemIndex[i]).style.backgroundImage="url('./images/day-li.png')";
                // alert(i)
            }
        }
        if(itemIndex&&itemIndex[index]){
            // alert(1)
            document.getElementById(itemIndex[index]).style.backgroundImage="url('./images/day-li-active.png')"
        }

        return me;
    };

    mod.clearCss = function () {
		var me = mod;
		// elem = evm.$('monthlist').getElementsByClassName(listName);
		// for (i = 0; i < length; i++) {
		// 	elem[i].className = listName;
        // }
        for(var i =0;i<15;i++){
            document.getElementById(itemIndex[i]).style.backgroundImage="url('./images/day-li.png')";
        }
		return me;
	};

    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左
            if (index > 0) {
                index--;
            }
            mod.toggleFocus();
        } else if (directive == 'forwards') { //右
            if (length - 1 > index) {
                index++;
            }
            mod.toggleFocus();
        }

        return me;
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
            if (index > 4) {
                index -= 4;
            } else {
                index = 0;
            }
            mod.toggleFocus();
        } else if (directive == 'down') { //下
            if (index < 11) {
                index += 4;
                mod.toggleFocus();
            } else{
                right1.clearCss()
                cmsIndex.handle("bottom1");
                bottom1.toggleFocus();
            }
        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                history.go(-1);
                return false;
                break;
            case 'enter':
                EVM.cookie.setValue('indexIndex', index);
                window.location.href = 'recordhistory.html?currentdate='+itemIndex[index];
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();




cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'bottom1',
    modNum: cmsIndex.moduleGuid(),
    map: {
        right1: 0
    }
};

var bottom1 = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //右侧选中地址位置
        listName = 'list-li',
        navName = 'navContain',
        length = 2,
        containerId = 'right',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'huodong';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function () {
        var me = mod;
        if (index == 0) {

            document.getElementById("btn1").style.backgroundImage = "url(./images/enterQuestion_active.png)";
            document.getElementById("btn2").style.backgroundImage = "url(./images/exit.png)";
        } else if (index == 1) {
            document.getElementById("btn1").style.backgroundImage = "url(./images/enterQuestion.png)";
            document.getElementById("btn2").style.backgroundImage = "url(./images/exit_active.png)";
        }
        return me;
    };
    mod.clearCss = function () {
		var me = mod;
        // document.getElementById("btn1").style.backgroundImage = "url(./images/enterQuestion.png)";
        // document.getElementById("btn2").style.backgroundImage = "url(./images/exit.png)";
		return me;
	};

    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左
            index=0;
        } else if (directive == 'forwards') { //右
            index=1;
        }

        mod.toggleFocus();
        return me;
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
            document.getElementById("btn1").style.backgroundImage = "url(./images/enterQuestion.png)";
            document.getElementById("btn2").style.backgroundImage = "url(./images/exit.png)";
            cmsIndex.handle('right1');
            right1.toggleFocus()
        } else if (directive == 'down') { //下
            // if (index < 11) {
            //     index += 4;
            // } else {
            //     index = 14;
            // }
        }
        // mod.toggleFocus();
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                history.go(-1);
                return false;
                    // if (!backurl) {
                    //     window.location.href = "./index.html"
                    //     // window.location.href = "./record.html"
                    // } else {
                    //     window.location.href = decodeURIComponent(backurl)
                    // }
                break;
            case 'enter':
                if(index==0){
                    window.location.href = 'da-ti.html'
                }else if(index==1){
                    history.go(-1)
                    // if (!backurl) {
                    //     history
                    //     // window.location.href = "./record.html"
                    // } else {
                    //     window.location.href = decodeURIComponent(backurl)
                    // }
                }
                // EVM.cookie.setValue('indexIndex', index);
                // window.location.href = data[index].url;
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();

cmsIndex.run();